package com.hxl.demo.controller;

import com.hxl.demo.entity.Staff;
import com.hxl.demo.entity.TrainingRecord;
import com.hxl.demo.service.Impl.TrainingRecodeServiceImpl;
import com.hxl.demo.tools.Common;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class TrainingRecordController {

    @Resource
    private TrainingRecodeServiceImpl trainingRecodeServiceImpl;

    @RequestMapping("/beginStudy")
    public Object beginStudy(
            @RequestParam(name = "contentId") Integer contentId,
            @RequestParam(name = "beginTime") String beginTime,
            HttpServletRequest request
    ) throws ParseException {


        if(contentId==null){
            return Common.returnJsonFail("文件名称不能为空",400);
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = dateFormat.parse(beginTime);
        HttpSession session = request.getSession();
        Integer useId= (Integer) session.getAttribute("userInfo");
        if(useId==null){
            return Common.returnJsonFail("请重新登录",400);
        }

        Integer id=trainingRecodeServiceImpl.beginStudy(useId,contentId,date);

        if(id>0){
            return Common.returnJsonFail("操作成功",200);
        }
            return Common.returnJsonFail("操作失败",400);

    }
    @RequestMapping("/endStudy")
    public Object endStudy(
            @RequestParam(name = "contentId") Integer contentId,
            @RequestParam(name = "trainingTime") Integer trainingTime,
            @RequestParam(name = "endTime") String endTime,
            HttpServletRequest request
    ) throws ParseException {
        if(contentId==null){
            return Common.returnJsonFail("文件名称不能为空",400);
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = dateFormat.parse(endTime);
        HttpSession session = request.getSession();
        Integer useId = (Integer) session.getAttribute("userInfo");
        if (useId != null) {
            List<Map<String,Object>> trainingRecord = trainingRecodeServiceImpl.getRecodeById(useId, contentId);

            List<Map<String,Object>> trainingRecordCount = trainingRecodeServiceImpl.getRecodeByCount(useId, contentId);
            Integer cumulativeTime=0;


            if(trainingRecord==null || trainingRecord.size()<0){
                return Common.returnJsonFail("操作失败",400);
            }
//            总学习时长
            if(trainingRecordCount!=null && trainingRecordCount.size()>0){
                 cumulativeTime= (int) trainingRecordCount.get(0).get("cumulative_time");
            }


            Integer count=trainingTime+cumulativeTime;

            Integer id= trainingRecodeServiceImpl.endStudy(Integer.valueOf(trainingRecord.get(0).get("id").toString()),date,trainingTime,count);

            if(id>0){
                return Common.returnJsonFail("操作成功",200);
            }else{
                return Common.returnJsonFail("操作失败",400);
            }
        }else{
            return Common.returnJsonFail("请重新登录",400);
        }

    }
}
